<?php

class MenuHelper extends Helper
{

  public function show(Menu $menu)
  {
    $output = $this->showMenu($menu, 0);
    return $this->output($output);
  }

  protected function showMenu(Menu $menu, $level)
  {
    $output = "<ul class=\"menu menu-$level\">";

    $output .= "<li class=\"menu-cap menu-cap-top menu-cap-$level menu-cap-top-$level\"></li>";

    $i = 0;
    while ($i < $menu->count()) {
      $output .= $this->showItem($menu->getItemAt($i), $level);
      $i ++;
    }

    $output .= "<li class=\"menu-cap menu-cap-bottom menu-cap-$level menu-cap-bottom-$level\"></li>";

    $output .= '</ul>';

    return $output;
  }

  protected function showItem(MenuItem $item, $level)
  {
    $active = $item->match(s::request()->route()) ? ' active' : '';
    $child_active = $item->childMatch(s::request()->route()) ? ' child-active' : '';

    $output = "<li class=\"menu-item menu-item-$level$active$child_active\">";

    /*$url = is_array($item->url) ? sy_get_param($item->url, 0) : $item->url;
    $get = is_array($item->url) ? sy_get_param($item->url, 1) : null;

    $label = is_array($item->label) ? sy_get_param($item->label, 0) : $item->label;
    $icon = is_array($item->label) ? sy_get_param($item->label, 1) : null;

    $_item = array(
      'route' => $url, 'params' => $get, 'label' => $label, 'icon' => $icon
    );

    $output .= $this->menu_item->show($_item);*/

    $output .= $this->menu_item->show($item);

    $output .= '</li>';

    if ($item->hasSubmenu()) {
      $output .= $this->showMenu($item->getSubmenu(), $level + 1);
    }

    return $output;
  }

}

?>